package com.example.fufudamowang.greendao;

import android.app.Activity;
import android.app.ListActivity;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.sax.StartElementListener;

import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SimpleCursorAdapter;

import android.os.Bundle;
import android.app.Dialog;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;

import com.example.fufudamowang.DaoMaster;
import com.example.fufudamowang.DaoSession;
import com.example.fufudamowang.Note;
import com.example.fufudamowang.NoteDao;
import android.content.Intent;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

   //DAO
    private SQLiteDatabase db;
    private DaoMaster daoMaster;
    private DaoSession daoSession;
    private NoteDao noteDao;
//List
    private Cursor cursor;
    private ListView aaa;
    private SimpleCursorAdapter adapter;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        aaa=(ListView)findViewById(R.id.L1);


        Builder dialog = new AlertDialog.Builder(MainActivity.this);
        dialog.setIcon(android.R.drawable.ic_dialog_info);
        dialog.setTitle("欢迎");
        dialog.setMessage("感谢您的使用");
        dialog.setPositiveButton("好的", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        dialog.create();
        dialog.show();
        InitDAO();// 初始化GreenDAO


        InitList();// 初始化List

    }

    private void InitDAO() {
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "notes-db",
                null);
        db = helper.getWritableDatabase();
        daoMaster = new DaoMaster(db);
        daoSession = daoMaster.newSession();
        noteDao = daoSession.getNoteDao();

    }


    private void InitList() {
        String textColumn = NoteDao.Properties.Title.columnName;
        String dateColunm = NoteDao.Properties.Date.columnName;
        String orderBy = dateColunm + " COLLATE LOCALIZED DESC";//智能排序
        cursor = db.query(noteDao.getTablename(), noteDao.getAllColumns(),
                null, null, null, null, orderBy);
        String[] from = { textColumn, dateColunm };
        int[] to = { R.id.text1, R.id.created };

        adapter = new SimpleCursorAdapter(this,
                R.layout.row, cursor, from, to);
        aaa.setAdapter(adapter);
        aaa.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//点击跳转修改页面
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(MainActivity.this,Edit.class);
                Bundle bundle=new Bundle();
                bundle.putLong("ID",id);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

//长按删除
        aaa.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                delete(id);

                Toast.makeText(MainActivity.this,"删除成功",Toast.LENGTH_SHORT).show();
                return false;
            }
        });
    }
    private void delete(long id){
        noteDao.deleteByKey(id);
        cursor.requery();

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu,menu);
        final MenuItem menuItem=menu.findItem(R.id.search_view);
        SearchView searchView=(SearchView)menuItem.getActionView();
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            //静态查找
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            //动态查找

            @Override
            public boolean onQueryTextChange(String newText) {
                if (newText.equals("")){
                    aaa.setAdapter(adapter);
                }
                String textColumn = NoteDao.Properties.Title.columnName;
                String dateColunm = NoteDao.Properties.Date.columnName;
                Cursor search_cursor=db.query(noteDao.getTablename(), noteDao.getAllColumns(),
                        "TITLE like ?", new String[] {"%"+newText+"%"}, null, null, null);
                String[] from = { textColumn, dateColunm };
                int[] to = { R.id.text1, R.id.created };

                SimpleCursorAdapter search_adapter = new SimpleCursorAdapter(MainActivity.this,
                        R.layout.row, search_cursor, from, to);
                aaa.setAdapter(search_adapter);

                return false;
            }
        });
        return super.onCreateOptionsMenu(menu);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId()==R.id.add){
            Intent intent=new Intent(MainActivity.this,insert1.class);
            startActivity(intent);


        }

        else if(item.getItemId()==R.id.close){
            Builder dialog = new AlertDialog.Builder(MainActivity.this);
            dialog.setIcon(android.R.drawable.ic_dialog_info);
            dialog.setTitle("再见");
            dialog.setMessage("期待与您的下次相遇");
            dialog.setPositiveButton("好的", new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();

                }
            });
            dialog.create();
            dialog.show();

        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onPostResume() {
        cursor.requery();
        super.onPostResume();
    }
}
